// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PicknPlaceStatusMessage.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Manipulation.Runtime
{
    using System;
    using System.Runtime.Serialization;
    using Microsoft.Robotics.Manipulation.PicknPlace;
    using Microsoft.Robotics.Numerics;
    using Microsoft.Robotics.PhysicalObjects;
    using Microsoft.Robotics.Runtime;

    /// <summary>
    /// Reports status of a pick and place task
    /// </summary>
    [DataContract]
    public class PicknPlaceStatusMessage : AgentMessage
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PicknPlaceStatusMessage" /> class
        /// The status message applies to both pick and place sub tasks. The statusEnum member clarifies
        /// the state
        /// </summary>
        /// <param name="initialHardwareMessage">Message from triggered this message</param>
        /// <param name="statusString">Description of the status</param>
        /// <param name="statusEnum">Enum of the status</param>
        /// <param name="manipulatedObjectDescription">Picked object</param>
        /// <param name="pickPlacePose">Object pose in pick or place location </param>
        /// <param name="retractPose">Retraction pose</param>
        /// <param name="requestMessage">Request message (Pick or place) responsible for last pick or place operation</param>
        public PicknPlaceStatusMessage(
            AgentMessage initialHardwareMessage,
            string statusString,
            PicknPlaceStatusEnum statusEnum,
            PhysicalObjectDescription manipulatedObjectDescription,
            Pose pickPlacePose,
            Pose retractPose,
            AgentMessage requestMessage = null)
            : base(initialHardwareMessage)
        {
            this.StatusString = statusString;
            this.StatusEnum = statusEnum;
            this.ManipulatedObjectDescription = manipulatedObjectDescription;
            this.PickPlacePose = pickPlacePose;
            this.RetractPose = retractPose;
            if (null != requestMessage)
            {
                this.OriginatingMessageId = requestMessage.OriginatingTime;
            }
            else
            {
                this.OriginatingMessageId = 0;
            }
        }

        /// <summary>
        /// Gets a string describing current state
        /// </summary>
        [DataMember]
        public string StatusString { get; private set; }

        /// <summary>
        /// Gets an enum indicating sub task status
        /// </summary>
        [DataMember]
        public PicknPlaceStatusEnum StatusEnum { get; private set; }

        /// <summary>
        /// Gets a description of the picked object
        /// </summary>
        [DataMember]
        public PhysicalObjectDescription ManipulatedObjectDescription { get; private set; }

        /// <summary>
        /// Gets object's pick or place location and orientation
        /// </summary>
        [DataMember]
        public Pose PickPlacePose { get; private set; }

        /// <summary>
        /// Gets the final retraction location and orientation
        /// </summary>
        [DataMember]
        public Pose RetractPose { get; private set; }

        /// <summary>
        /// Gets the Id of the message that initiated the last pick or place operation
        /// The special id 0 is reserved for when there is no last message ID
        /// </summary>
        [DataMember]
        public long OriginatingMessageId { get; private set; }

        /// <summary>
        /// Gets a value indicating whether the message contains a pick failure
        /// </summary>
        public bool IsPickFail
        {
            get
            {
                return this.StatusEnum == PicknPlaceStatusEnum.PickFailFalseNegative ||
                       this.StatusEnum == PicknPlaceStatusEnum.PickFailFalsePositive ||
                       this.StatusEnum == PicknPlaceStatusEnum.PickFailLargeError ||
                       this.StatusEnum == PicknPlaceStatusEnum.PickFailLowConfidence ||
                       this.StatusEnum == PicknPlaceStatusEnum.PickFailOutOfRange;
            }
        }

        /// <summary>
        /// Gets a value indicating whether the message contains a place failure
        /// </summary>
        public bool IsPlaceFail
        {
            get
            {
                return this.StatusEnum == PicknPlaceStatusEnum.PlaceFailOutOfRange;
            }
        }
    }
}
